using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;
using Konst3d.Efficiency.Suite;

namespace Konst3d.Efficiency.Suite.SparsedBinary
{
    internal interface IActiveDaysStorage
    {
        void UpdateDayOfWeek(int index, bool value);
        void UpdateDayOfMonth(int index, bool value);

        bool[] GetDaysOfWeek();
        bool GetDayOfWeek(int index);

        bool[] GetDaysOfMonth();
        bool GetDayOfMonth(int index);
    }

    internal class MonitoringActiveDays: IMonitoringActiveDays
    {
        #region IMonitoringActiveDays Members

        public NotifiableArray<bool> DaysOfWeek
        {
            get
            {
                return _daysOfWeek;
            }
        }

        public NotifiableArray<bool> DaysOfMonth
        {
            get
            {
                return _daysOfMonth;
            }
        }

        #endregion

        #region Constructor

        public MonitoringActiveDays(IActiveDaysStorage storage)
        {
            Contract.Requires(storage != null);

            _storage = storage;

            for (int i = 0; i < 7; ++i)
            {
                _daysOfWeek[i] = _storage.GetDayOfWeek(i);
            }

            for (int i = 0; i < 31; ++i)
            {
                _daysOfMonth[i] = _storage.GetDayOfMonth(i);
            }

            _daysOfWeek.ItemChanged += OnDayOfWeekChanged;
            _daysOfMonth.ItemChanged += OnDayOfMonthChanged;
        }

        #endregion // Constructor

        #region Event Handlers

        private void OnDayOfWeekChanged(int index, bool oldValue, bool newValue)
        {
            _storage.UpdateDayOfWeek(index, newValue);
        }

        private void OnDayOfMonthChanged(int index, bool oldValue, bool newValue)
        {
            _storage.UpdateDayOfMonth(index, newValue);
        }

        #endregion // Event Handlers

        #region Fields

        private IActiveDaysStorage _storage;
        private NotifiableArray<bool> _daysOfWeek = new NotifiableArray<bool>(7);
        private NotifiableArray<bool> _daysOfMonth = new NotifiableArray<bool>(31);

        #endregion // Fields
    }
}
